// pages/student/my-course/my-course.js
import { courses } from '../../../utils/mockData.js'

Page({
  data: {
    tabIndex: 0,
    tabs: ['进行中', '已报名', '已完成'],
    myCourses: [] // 我的课程列表,包含学习进度
  },
  
  onLoad() {
    this.loadMyCourses()
  },
  
  onShow() {
    // 每次显示页面时刷新数据
    this.loadMyCourses()
  },
  
  onPullDownRefresh() {
    this.loadMyCourses()
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  },
  
  loadMyCourses() {
    // 模拟从后端获取我的课程数据,包含学习进度
    // 实际应该调用API获取student_courses表的数据
    const myCourses = courses.slice(0, 3).map((course, index) => {
      // 计算总课时数
      let totalLessons = 0
      let completedLessons = 0
      
      if (course.chapters) {
        course.chapters.forEach(chapter => {
          if (chapter.lessons) {
            totalLessons += chapter.lessons.length
            // 模拟已完成的课时数
            completedLessons += Math.floor(chapter.lessons.length * (index === 0 ? 0.65 : index === 1 ? 0.30 : 0.95))
          }
        })
      }
      
      const progress = totalLessons > 0 ? Math.round((completedLessons / totalLessons) * 100) : 0
      const isCompleted = progress >= 95
      
      return {
        ...course,
        totalLessons,
        completedLessons,
        progress,
        isCompleted,
        lastStudyTime: this.getLastStudyTime(index),
        enrollTime: this.getEnrollTime(index)
      }
    })
    
    // 根据当前tab过滤课程
    const filteredCourses = this.filterCoursesByTab(myCourses, this.data.tabIndex)
    
    this.setData({
      myCourses: filteredCourses
    })
  },
  
  // 根据tab过滤课程
  filterCoursesByTab(courses, tabIndex) {
    switch(tabIndex) {
      case 0: // 进行中
        return courses.filter(c => c.progress > 0 && c.progress < 95)
      case 1: // 已报名
        return courses.filter(c => c.progress === 0)
      case 2: // 已完成
        return courses.filter(c => c.isCompleted)
      default:
        return courses
    }
  },
  
  // 获取最后学习时间(模拟)
  getLastStudyTime(index) {
    const times = ['2小时前', '昨天', '3天前']
    return times[index] || '未学习'
  },
  
  // 获取报名时间(模拟)
  getEnrollTime(index) {
    const times = ['2024-12-01', '2024-11-25', '2024-11-15']
    return times[index] || '2024-10-01'
  },
  
  switchTab(e) {
    const index = e.currentTarget.dataset.index
    this.setData({ tabIndex: index })
    this.loadMyCourses()
  },
  
  goToCourse(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/student/course-detail/course-detail?id=${id}`
    })
  },
  
  continueLearning(e) {
    const id = e.currentTarget.dataset.id
    // 继续学习,跳转到视频播放页面
    wx.navigateTo({
      url: `/pages/student/replay/replay?courseId=${id}`
    })
  },
  
  enterLiveRoom(e) {
    const id = e.currentTarget.dataset.id
    const course = this.data.myCourses.find(c => c.id === id)
    
    if (course && course.is_live) {
      wx.navigateTo({
        url: `/pages/student/live-room/live-room?courseId=${id}`
      })
    } else {
      wx.showToast({
        title: '当前无直播',
        icon: 'none'
      })
    }
  },
  
  watchReplay(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/student/replay/replay?courseId=${id}`
    })
  }
})
